<?php
	require 'config.php';
	session_start();
	$_param = $_REQUEST['param'];
	/*if($_cual == "ins")
	{
		$resultset = mysql_query($_REQUEST['query'],$conexion);
		mysql_close();
		echo json_encode("ok");
	}
	else*/ if($_param == "buscar")
	{
			
		$resultset = mysql_query($_REQUEST['query'],$conexion);
		
		if(!$resultset)
		{
			mysql_close();
			echo json_encode('error en el query: ' . mysql_error());
		}
		elseif(!mysql_num_rows($resultset))
		{
			mysql_close();
			echo json_encode('Sin resultados');
		}
		else
		{
			$data = array();
			while ($row = mysql_fetch_array($resultset))
			 {
			    $data[] = $row['idusuarios'];
    			$data[] = $row['pass'];
    			$_SESSION["id"] =$row['idusuarios'];
    			$_SESSION["nombre"] =$row['nombre'];
    			$_SESSION["apellido"] =$row['apellido'];
    			$_SESSION["user"] =$row['user'];
    			$_SESSION["solicitudes"] =$row['solicitudes'];
    			$_SESSION["p"] = "noretorno";
    			$_SESSION["rol"] = $row['rol'];
    			$_SESSION["activo"] = $row['activo'];
    			
			}
			$fp = fopen("log.txt","a");
			fwrite($fp, "User: ". $_SESSION['nombre']." \t ".date("d-m-Y H:i:s")."  \t ".getRealIP(). PHP_EOL);
			fclose($fp);
		
			echo json_encode($data);
		}
	}
	else if($_param == "check_in")
	{
		if (empty($_SESSION['nombre'])) 
				echo "<meta http-equiv='Refresh' content='1;url=index.php'>";
	}
	function getRealIP() {
		if (!empty($_SERVER['HTTP_CLIENT_IP']))
			return $_SERVER['HTTP_CLIENT_IP'];
		 
		if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
			return $_SERVER['HTTP_X_FORWARDED_FOR'];
		 
		return $_SERVER['REMOTE_ADDR'];
	}
?>